#include <game/flow/FlowDefinition.h>
#include <game/flow/Manager.h>
#include <game/ResourceReferenceDefinition.h>

#include <xml/Stream.h>
#include <resource/Manager.h>

ARELLO_GAME_NAMESPACE_DECL_START
namespace flow {

FlowDefinition::FlowDefinition()
	: flowManager(new Manager())
{ }

FlowDefinition::~FlowDefinition()
{ }

bool FlowDefinition::on_node(xml::Provider * prov, const std::string & name)
{
	if (name == "Resource")
	{	
		ResourceReferenceDefinition def;
		if (!prov->parse_object(def.GetLoader()))
			return false;
		
		this->flowManager.reset(new flow::Manager());

		std::auto_ptr<resource::Resource> flowRes(resource::Manager::create_resource(def.GetPath()));
		if (!xml::StreamResource(flowRes.get()).load(&xml::Root(this->flowManager->GetLoader(), "Flow")))
		{
			this->flowManager.reset();
			THROW(GluckBadResource(__FUNCTION__, "load() failed", def.GetPath()));
		}

		return true;
	}

	return false;
}

std::auto_ptr<Manager> FlowDefinition::ReleaseFlowManager()
{
	return this->flowManager;
}

} // namespace flow
ARELLO_GAME_NAMESPACE_DECL_END